﻿//--------------------------------------------------------------------------
//
//  File:        StoneControl.cs
//
//  Coder:       bigeagle@gmail.com
//
//  Date:        2013/1/28
//
//  Description: 棋子控件类
//  
//  History:     2013/1/27 created by bigeagle  
//
//--------------------------------------------------------------------------

using Bigeagle.Portable.BoardGames;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// “用户控件”项模板在 http://go.microsoft.com/fwlink/?LinkId=234236 上提供

namespace Bigeagle.Metro.Games.Controls
{
    /// <summary>
    /// 棋子控件
    /// </summary>
    public sealed partial class StoneControl : UserControl
    {
        #region 成员变量及属性
        /// <summary>
        /// 绑定的棋子
        /// </summary>
        Stone _Stone;

        /// <summary>
        /// 设置或获得绑定的棋子
        /// </summary>
        public Stone BindingStone
        {
            get
            {
                return _Stone;
            }
            set
            {
                _Stone = value;
                OnStoneChanged(new StoneEventArgs(_Stone));
            }
        }

        /// <summary>
        /// 棋子改变委托
        /// </summary>
        private StoneEventHandler _StoneChanged = null;

        /// <summary>
        /// 引发棋子改变事件
        /// </summary>
        /// <param name="e"></param>
        private void OnStoneChanged(StoneEventArgs e)
        {
            Draw();
            if (_StoneChanged != null)
            {
                _StoneChanged(this, e);
            }
        }
        #endregion//end成员变量及属性

        //#region 依赖属性
        //#region 是否显示Mark属性
        //public static readonly DependencyProperty IsShowMarkProperty =
        //    DependencyProperty.Register("IsShowMark", typeof(bool), typeof(StoneControl)
        //    , new PropertyMetadata(false, new PropertyChangedCallback(OnIsShowMarkChanged)));

        //public bool IsShowMark
        //{
        //    get
        //    {
        //        return (bool)GetValue(IsShowMarkProperty);
        //    }
        //    set
        //    {
        //        SetValue(IsShowMarkProperty, value);
        //    }
        //}
        //private static void OnIsShowMarkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    StoneControl control = (StoneControl)d;
        //    control.polygonMark.Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed;

        //}

        //#endregion
        //#endregion//end 依赖属性

        /// <summary>
        /// 绘图
        /// </summary>
        private void Draw()
        {
            //Uri backUri = new Uri(this.BaseUri , string.Format(@"/Assets/{0}shadow.png"
            //    , _Stone.Side == Stone.Sides.Black ? "black" : "white"));
            //Uri stoneUri = new Uri(this.BaseUri , string.Format(@"/Assets/{0}stone52.png"
            //    , _Stone.Side == Stone.Sides.Black ? "black" : "white"));
    //        Uri backUri = new Uri(string.Format(@"ms-appx:/Bigeagle.Metro.Games.Controls/Assets/{0}shadow.png"
    //, _Stone.Side == Stone.Sides.Black ? "black" : "white"));
            Uri stoneUri = new Uri(string.Format(@"ms-appx:/Bigeagle.Metro.Games.Controls/Assets/{0}stone52.png"
                , _Stone.Side == Stone.Sides.Black ? "black" : "white"));

            //brushBackground.ImageSource = new BitmapImage(backUri);
            imageStone.Stretch = Stretch.UniformToFill;
            imageStone.Source = new BitmapImage(stoneUri);
            //System.Drawing.Bitmap[] bitmaps = new System.Drawing.Bitmap[]
            //{Properties.Resources.blackshadow 
            //    , Properties.Resources.blackstone52 
            //    , Properties.Resources.whiteshadow
            //    ,Properties.Resources.whitestone52};
            //int stoneIndex = _Stone.Side == Stone.Sides.Black ? 1 : 3;
            //MemoryStream stoneStream = new MemoryStream();
            //bitmaps[stoneIndex].Save(stoneStream, System.Drawing.Imaging.ImageFormat.Png);
            //BitmapImage stoneImage = new BitmapImage();
            //stoneImage.BeginInit();
            //stoneImage.StreamSource = stoneStream;
            //imageStone.Source = stoneImage;
            //stoneImage.EndInit();
            //stoneIndex -= 1;
            //MemoryStream backStream = new MemoryStream();
            //bitmaps[stoneIndex].Save(stoneStream, System.Drawing.Imaging.ImageFormat.Png);
            //BitmapImage backImage = new BitmapImage();
            //backImage.BeginInit();
            //backImage.StreamSource = backStream;
            //brushBackground.ImageSource = backImage;
            //backImage.EndInit();
            //stoneStream.Close();
            //backStream.Close();
            ////brushBackground.ImageSource = new BitmapImage(backImageUri);
            ////imageStone.Source = new BitmapImage(ImageUri);

        }

        /// <summary>
        /// 棋子改变事件
        /// </summary>
        public event StoneEventHandler StoneChanged
        {
            add
            {
                _StoneChanged += value;
            }
            remove
            {
                _StoneChanged -= value;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public StoneControl()
        {
            InitializeComponent();
            _Stone = new Stone();
            //Draw();
        }

        /// <summary>
        /// 控件载入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            double w = canvasMain.ActualWidth;
            double h = canvasMain.ActualHeight;
            imageStone.Width = w;
            imageStone.Height = h;
            //Draw();
            //polygonMark.Points.Clear();
            //polygonMark.Points.Add(new Point(w/5*2, h/5*1)) ;
            //polygonMark.Points.Add(new Point(w/5*4, h/2)) ;
            //polygonMark.Points.Add(new Point(w/5*2, h/5*4));
        }//end method
    }//end class
}//end namespace
